import sys

import nacos
import json
import time

"""
pip install nacos-sdk-python
"""

SERVER_ADDRESSES = "116.62.129.53:8848"
NAMESPACE = "6624edaa-7683-4a13-b740-a716ad60d822"

client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE)

# get config
data_id = "dev_config"
group = "DEFAULT_GROUP"

# 全局服务配置
data_s = client.get_config(data_id, group)
if not data_s:
    print('没有服务器配置')
    sys.exit()

print('data_s', 'sd', data_s, type(data_s))
server_config = json.loads(data_s)

print('original data:', server_config, type(server_config))


# 当服务配置发生变化
def config_update(data):
    global server_config
    print('ddd', data, type(data))
    server_config = data['content']
    print('new data->', server_config)


# 监听服务配置变化
client.add_config_watcher(data_id, group, config_update)


# 模拟web服务一直运行
while True:
    time.sleep(1)
